<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/11/27
 * Time: 15:28
 */

namespace App\Http\Controllers\Api\Shop;

use App\Http\Controllers\Api\BaseAdminController;
use App\Http\Requests\Order\CarrierRequest;
use App\Http\Requests\Order\DeliverOrderRequest;
use App\Http\Requests\Order\GetOrderDetailRequest;
use App\Http\Requests\Order\GetOrderPagePageRequest;
use App\Http\Requests\Order\RegionListRequest;
use App\Http\Requests\Order\ShipDetailsRequest;
use App\Http\Requests\Order\UpdateReceiverRequest;
use App\Http\Transformers\Order\CarrierListTransformer;
use App\Http\Transformers\Order\GetOrderDetailTransformer;
use App\Http\Transformers\Order\GetOrderPageTransformer;
use App\Http\Transformers\Order\RegionListTransformer;
use App\Repositories\OrderRepository;
use App\Services\ProcessService;

/**
 * 订单相关
 * Class OrderController
 * @package App\Http\Controllers\Api\Shop
 */
class OrderController extends BaseAdminController
{
    use ProcessService;

    /**
     * @var OrderRepository 注入的数据模型
     */
    protected $orderRepository;

    /**
     * OrderController constructor.
     * @param OrderRepository $orderRepository
     */
    public function __construct(OrderRepository $orderRepository)
    {
        $this->orderRepository = $orderRepository;
    }

    /**
     * 获取订单列表
     * @param GetOrderPagePageRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getOrderPage(GetOrderPagePageRequest $request){
        $limit = $request->get('limit');
        $data = $request->except(['limit', 'page']);

        $banner = $this->orderRepository->getOrderPageRepository($data, $limit);

        return $this->response->paginator($banner, new GetOrderPageTransformer());
    }

    /**
     * 获取订单详情
     * @param GetOrderDetailRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getOrderDetail(GetOrderDetailRequest $request){
        $banner = $this->orderRepository->getOrderDetailRepository($request->id);

        return $this->response->item($banner, new GetOrderDetailTransformer());
    }

    /**
     * 获取地域列表
     * @param RegionListRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function regionList(RegionListRequest $request){
        $parentId = $request->get('parentId', 0);

        //否则请求数据
        $list = $this->orderRepository->regionListRepository($parentId);

        return $this->response->collection($list, new RegionListTransformer());
    }

    /**
     * 获取快递公司
     * @param CarrierRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getCarrierList(CarrierRequest $request){
        //否则请求数据
        $list = $this->orderRepository->getCarrierListRepository();

        return $this->response->collection($list, new CarrierListTransformer());
    }

    /**
     * 订单发货
     * @param DeliverOrderRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deliverOrder(DeliverOrderRequest $request){
        $datum = $request->all();
        if(!$datum || is_null($datum)){
            $this->response->errorBadRequest('请选择订单！');
        }
        $deliver = $this->orderRepository->deliverOrderRepository($datum);

        return $this->response->accepted($deliver, 'success');
    }

    /**
     * 获取物流数据
     * @param ShipDetailsRequest $request
     * @return string
     */
    public function shipDetails(ShipDetailsRequest $request){

        //TODO
        $shipNo = $request->get('shipNo', '70791761292451');//$request->get('shipNo', null);
        $getShipData = $this->shipsQuery($shipNo);//var_dump($getShipData);exit;
        if(!$getShipData){
            $this->response->errorInternal('获取物流数据出错，请再次获取！');
        }
        $status = data_get($getShipData, 'status');
        if($status == 400){
            $this->response->errorBadRequest(data_get($getShipData, 'message'));
        }

        return \GuzzleHttp\json_encode(
            ['com'=>data_get($getShipData, 'com'),
                'cname'=>data_get($getShipData, 'cname'),
                'status'=>data_get($getShipData, 'status'),
                'list'=>data_get($getShipData, 'list')
            ]);
    }

    /**
     * 修改收货人信息
     * @param UpdateReceiverRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateReceiver(UpdateReceiverRequest $request){
        $data = $request->all(['zip', 'receiverProvince', 'receiverDetailAddress', 'receiverCity', 'receiverAddress',
            'phone', 'orderId', 'consignee']);

        if($this->orderRepository->updateReceiverRepository($data)){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('修改收货信息失败!');
    }

}
